<?php

namespace App\Console\Commands;

use App\Services\Base\CacheService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class ClearCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'clear:all';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->databaseClear();
        $this->cacheClear();
        return Command::SUCCESS;
    }

    public function databaseClear()
    {
        $data = [
            'user' => [
                "truncate table user_trees",
                "truncate table users",
            ],
            'order' => [

            ],
            'statistics' => [

            ],
            'chain' => [

            ]
        ];

        foreach ($data as $k => $v) {
            foreach ($v as $kk => $vv) {
                DB::update($vv);
            }
        }

        DB::update("ALTER TABLE users AUTO_INCREMENT = 80000000");
    }

    public function cacheClear()
    {
        CacheService::del(R_USER_INFO);
        CacheService::del(R_LOGIN_TOKEN_INFO);
        CacheService::del(R_LOGIN_TOKEN_HASH);
        CacheService::del(R_LOGIN_TOKEN_HASH_MULTI);
        CacheService::del(R_IP);
    }
}
